﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;

namespace MS.InformationManagement.BLL.ShippingControlTemplates
{
    public class CreateItemDistributionListCommand : IBaseCommand
    {
        #region Fields

        IEnumerable<PlanillasDTO> _foundItems;
        List<int> _itemsId = new List<int>();

        #endregion

        #region Constructors

        public CreateItemDistributionListCommand(IEnumerable<PlanillasDTO> foundItems)
        {
            _foundItems = foundItems;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foreach (var entity in _foundItems)
                {
                    Distribucion element = new Distribucion
                    {
                        NoRadicado = entity.NoRadicado,
                        RecibidoInterno = false,
                        UsuarioAsignado = entity.UsuarioAsignado
                    };
                    dc.Distribucion.InsertOnSubmit(element);
                    dc.SubmitChanges();
                    _itemsId.Add(element.Id.Value);
                }
            }
        }

        public void Undo()
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foreach (int id in _itemsId)
                {
                    var entity = (from item in dc.Distribucion
                                  where item.Id == id
                                  select item).FirstOrDefault();

                    dc.Distribucion.DeleteOnSubmit(entity);
                    dc.SubmitChanges();
                }
            }
        }

        #endregion
    }
}
